/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
  if(s.length == 0)
    return 0;
  // 1.中心扩散
  let length = 1, resultStr = s[0], tempStr = "";
  for(let k = 1; k < s.length; k++) {
    let i = k - 1, j = k + 1;
    tempStr = s[k];
    // 判断左边是否相同
    while(i >= 0 && s[i] == s[i + 1]) {
      tempStr = s[i] + tempStr;
      i--;
    }
    if(j >= s.length) {
      if(tempStr.length > length) {
        length = tempStr.length;
        resultStr = tempStr;
      }
      break;
    };
    while(j < s.length && s[j - 1] == s[j]) {
      tempStr = tempStr + s[j];
      j++;
    }
    while(i >= 0 && j < s.length && s[i] == s[j]) {
      tempStr = s[i] + tempStr + s[j];
      i--;
      j++;
    }
    if(tempStr.length > length) {
      length = tempStr.length;
      resultStr = tempStr;
    }
  }
  return resultStr;
};
let s = "babad";
console.log(longestPalindrome(s));
s = "cbbd";
console.log(longestPalindrome(s));
s = "bb";
console.log(longestPalindrome(s));
s = "anugnxshgonmqydttcvmtsoaprxnhpmpovdolbidqiyqubirkvhwppcdyeouvgedccipsvnobrccbndzjdbgxkzdbcjsjjovnhpnbkurxqfupiprpbiwqdnwaqvjbqoaqzkqgdxkfczdkznqxvupdmnyiidqpnbvgjraszbvvztpapxmomnghfaywkzlrupvjpcvascgvstqmvuveiiixjmdofdwyvhgkydrnfuojhzulhobyhtsxmcovwmamjwljioevhafdlpjpmqstguqhrhvsdvinphejfbdvrvabthpyyphyqharjvzriosrdnwmaxtgriivdqlmugtagvsoylqfwhjpmjxcysfujdvcqovxabjdbvyvembfpahvyoybdhweikcgnzrdqlzusgoobysfmlzifwjzlazuepimhbgkrfimmemhayxeqxynewcnynmgyjcwrpqnayvxoebgyjusppfpsfeonfwnbsdonucaipoafavmlrrlplnnbsaghbawooabsjndqnvruuwvllpvvhuepmqtprgktnwxmflmmbifbbsfthbeafseqrgwnwjxkkcqgbucwusjdipxuekanzwimuizqynaxrvicyzjhulqjshtsqswehnozehmbsdmacciflcgsrlyhjukpvosptmsjfteoimtewkrivdllqiotvtrubgkfcacvgqzxjmhmmqlikrtfrurltgtcreafcgisjpvasiwmhcofqkcteudgjoqqmtucnwcocsoiqtfuoazxdayricnmwcg";
console.log(longestPalindrome(s));
s = "aaaaa";
console.log(longestPalindrome(s));
s = "abcba";
console.log(longestPalindrome(s));
// let a = "123456";
// console.log(a.slice(1, 3));
// console.log(a);
